package com.subaiwang.controller;

import com.alibaba.fastjson.JSON;
import com.subaiwang.pojo.Pages;
import com.subaiwang.service.video.VideoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/video")
public class VideoController {

    @Resource
    private VideoService videoService;

    /**
     * 视频管理  --分类管理
     *
     * @return
     */
    @RequestMapping("/vodeo_list.html")
    public String product_category_add(HttpSession session){
        //设置一个页码
        int pageNo=1;
        Pages pages=new Pages();
        pages.setPageNo(pageNo);
        int count=videoService.VideoCount(null,null);
        pages.setPageCount(count);
        videoService.getVideo(null,null,pages);
        session.setAttribute("count",count);
        session.setAttribute("list",pages);
        return "vodeo_list";
    }

    /**
     * 视频管理  --分类管理  分页显示
     * @return
     */
    @RequestMapping(value = "/VideoListajax",method = RequestMethod.POST)
    @ResponseBody
    public Object doPost2(HttpSession session,
                          @RequestParam(value = "classify",required = false) String classify,
                          @RequestParam(value = "idValue",required = false) String idValue,
                          @RequestParam(value = "pageNo",required = false,defaultValue = "1") int pageNo){
        int pageNoStr=pageNo;
        int pageNo2=1;
        if (pageNoStr==0) {
            pageNo2=1;
        } else {
            pageNo2=pageNoStr;
        }

        Pages pages = new Pages();
        pages.setPageNo(pageNo2);
        int count=videoService.VideoCount(classify,idValue);
        pages.setPageCount(count);
        videoService.getVideo(classify,idValue,pages);
        session.setAttribute("count",count);
        session.removeAttribute("list");
        session.setAttribute("list",pages);
        String jsonarray = JSON.toJSONString(pages);
        return jsonarray;
    }
}
